﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using Castle.Windsor;
using Castle.MicroKernel.Registration;
using Domain.NHibernate;
using OS.Infrastructure.Common;
using OS.Infrastructure.Domain;
using OS.Infrastructure.NHibernate;
using OS.Infrastructure.NHibernate.Linq;
using OS.Infrastructure.NHibernate.UnitOfWorkAware;
using OS.Infrastructure.Domain.UnitOfWork;
using ShortUrl.Constraints;
using ShortUrl.Controllers;
using ShortUrl.Models;
using Domain;

namespace ShortUrl
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute("My", "{key}", new { controller = "Home", action = "RedirectKey" },
                new { key = new ExpectedValuesConstraint() });
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            ControllerBuilder.Current.SetControllerFactory(typeof(ContainerControllerFactory));

        }
    }
    public class ContainerControllerFactory : DefaultControllerFactory
    {
        private readonly WindsorContainer container;

        public ContainerControllerFactory()
        {
            container = new WindsorContainer();
            container.Register(
               AllTypes.Of(typeof(IController))
                    .FromAssembly(typeof(HomeController).Assembly)
                );
            container.Register(
               AllTypes.Of(typeof(IController))
                    .FromAssembly(typeof(AccountController).Assembly)
                );
            container.Register(Component.For<ISessionProvider>().ImplementedBy<SessionProvider>());
            container.Register(Component.For<INHibernateInitializer>().ImplementedBy<NHInitializer>());
            container.Register(Component.For<IUnitOfWorkFactory>().ImplementedBy<NHibernateUnitOfWorkFactory>());
            container.Register(Component.For<IAccountRepository>().ImplementedBy<AccountRepository>());
            container.Register(Component.For<IUrlRepository>().ImplementedBy<UrlRepository>());
            container.Register(Component.For<ILinqProvider>().ImplementedBy<NHibernateLinqProvider>());
            container.Register(Component.For<IMembershipService>().ImplementedBy<AccountMembershipService>());
            IoC.Init(container);
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType != null)
                return container.Resolve(controllerType) as IController;
            else return null;
        }
    }
}